#!/bin/bash
# test cgclear with multiple config files

. `dirname $0`/../testenv.sh

# start a process to torture
/bin/sleep 10000 &
PID=$!

# STEP1: parse three config files and cgclear them at once (in reverse order!)
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_1.conf` \
	|| die "STEP1: cgconfigparser multi_1 failed"
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_2.conf` \
	|| die "STEP1: cgconfigparser multi_2 failed"
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_3.conf` \
	|| die "STEP1: cgconfigparser multi_3 failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep 1/2/3 <$TMP/groups.log >/dev/null \
	|| die "STEP1: cannot find group '1/2/3'"

# clear it all
$TOOLSDIR/cgclear -e -l `prepare_config cgclear/cgclear_config_multi_1.conf` \
	-l `prepare_config cgclear/cgclear_config_multi_2.conf` \
	-l `prepare_config cgclear/cgclear_config_multi_3.conf` \
	|| die "STEP1: cgclear failed"

# check all is gone
$TOOLSDIR/lscgroup 2>/dev/null \
	&& die "STEP1: something is mounted after cgclear"


# STEP2: same as step1, but in wrong order - cgclear should fail
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_1.conf` \
	|| die "STEP2: cgconfigparser multi_1 failed"
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_2.conf` \
	|| die "STEP2: cgconfigparser multi_2 failed"
$TOOLSDIR/cgconfigparser -l `prepare_config cgclear/cgclear_config_multi_3.conf` \
	|| die "STEP2: cgconfigparser multi_3 failed"
$TOOLSDIR/lscgroup > $TMP/groups.log
grep 1/2/3 <$TMP/groups.log >/dev/null \
	|| die "STEP2: cannot find group '1/2/3'"

# clear it all
$TOOLSDIR/cgclear -e -l `prepare_config cgclear/cgclear_config_multi_3.conf` \
	-l `prepare_config cgclear/cgclear_config_multi_2.conf` \
	-l `prepare_config cgclear/cgclear_config_multi_1.conf` \
	|| die "STEP2: cgclear failed"

# check that cpu,cpuacct:/1/2 still exists
$TOOLSDIR/lscgroup > $TMP/groups.log
grep 1/2 <$TMP/groups.log >/dev/null \
	|| die "STEP2: cannot find group '1/2 after cgclear"

$TOOLSDIR/cgclear


cleanup
kill -9 $PID
exit 0
